<?php
class UserModel extends BaseModel {
	public function __construct() {
		parent::__construct ();
	}
	public function create() {
		$query = 'desc Activities';
		$rs = $this->_conn->select ( $query );
		// var_dump($rs);
		foreach ( $rs as $column ) {
			echo 'public ', '$', $column ['Field'], ';<br />';
		}
	}
	public function isEmailExists($email) {
		$query = "SELECT (id) from Users" . LB;
		$query .= "where email=:email";
		$param = array (
				"email" => $email 
		);
		$map [] = array (
				"email",
				"string" 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return true;
		}
		return false;
	}
	public function isUsernameExists($username) {
		$query = "SELECT (id) from Users" . LB;
		$query .= "where username=:username";
		$param = array (
				"username" => $username 
		);
		$map [] = array (
				"username",
				"string" 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return true;
		}
		return false;
	}
	public function isNickExists($nick) {
		$query = "SELECT (id) from Users" . LB;
		$query .= "where nick=:nick";
		$param = array (
				"nick" => $nick 
		);
		$map [] = array (
				"nick",
				"string" 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return true;
		}
		return false;
	}
	public function isColumnExists($colname, $val) {
		if (empty ( $colname )) {
			return false;
		}
		$query = "SELECT (id) from Users" . LB;
		$query .= "where :col=:val";
		$param = array (
				"col" => $colname,
				"val" => $val 
		);
		$map [] = array (
				"col",
				"string" 
		);
		$map [] = array (
				"val",
				"string" 
		);
		$resultSet = $this->_conn->select ( $query, $param, $map );
		if (count ( $resultSet )) {
			return true;
		}
		return false;
	}
	public function getUser(array $fieldsToGet, array $condition, array $typemap = null) {
		$fields = implode ( $fieldsToGet, ',' );
	}
	
	/**
	 * 注册用户
	 *
	 * @param array $fields        	
	 * @param array $typemap        	
	 */
	public function register(array $fields, array $map = null) {
		$password = $fields ["password"];
		$salt = GF::GenerateSalt ( 40 );
		$salted_pass = GF::saltedPassword($password, $salt, 0);
		
		$query = "INSERT INTO users SET 
		email=:email, 
		nick=:nick, 
		password=:password, 
		salt=:salt, 
		group_id=:groupid,
		ts_created=:now,
		ip_created=:ip
		";
		$params = array (
				"email" => $fields ["email"],
				"nick" => $fields ["nick"],
				"password" => $salted_pass,
				"salt" => addcslashes ( $salt, '\\' ), // 这么处理是因为反斜线在进入数据库的时候被转义掉
				"groupid" => $fields ["groupid"],
				"now" => time (),
				"ip" => GF::getRemoteIP () 
		);
		$map [] = array (
				"email",
				"string" 
		);
		$map [] = array (
				"nick",
				"string" 
		);
		$map [] = array (
				"password",
				"string" 
		);
		$map [] = array (
				"salt",
				"string" 
		);
		$map [] = array (
				"groupid",
				"int" 
		);
		$map [] = array (
				"now",
				"int" 
		);
		$map [] = array (
				"ip",
				"string" 
		);
		$uid = $this->_conn->insert ( $query, $params, $map );
		return $uid;
	}

	public function save(array $fields, array $values, array $typemap = null) {
		$query = "";
		foreach ( $fields as $k ) {
			$query .= $k . "=" . ":$k";
		}
		echo $query;
		return false;
	}
}
